<template>
  <Filing v-bind:queryParam="queryParam" refreshUrl="filefiling" v-bind:data="data" v-on:filing="filing()" v-on:initData="initData"></Filing>
</template>

<script>
  import http from '../utils/http.js'
  import store from '../utils/store.js'
  import common from '../utils/common.js'
  import Filing from './Filing'
  export default {
    name: 'FileFiling',
    components: {
      Filing,
    },
    methods: {
      init() {
        this.updateQueryParam(this.$route.params.dirPath, this.$route.params.type, this.$route.params.pageNum,
                              this.$route.params.pageSize, this.$route.params.index, this.$route.params.sessionId);
        this.initData();
      },
      updateQueryParam(dirPath, type, pageNum, pageSize, index, sessionId) {
        this.queryParam.dirPath = dirPath ? dirPath : this.queryParam.dirPath;
        this.queryParam.fileType = type ? type : this.queryParam.fileType;
        this.queryParam.pageNum = pageNum ? parseInt(pageNum) : this.queryParam.pageNum;
        this.queryParam.pageSize = pageSize ? pageSize : this.queryParam.pageSize;
        this.queryParam.index = parseInt(index);
        this.queryParam.sessionId = sessionId ? sessionId : this.queryParam.sessionId;
        this.queryParam.fromModule = "FileManage";
      },
      initData() {
        // 发送请求
        let that = this;
        http.post('/filing/detail', this.queryParam, (responseData) => {
          that.filingParam.filePath = responseData.filePath;
          
          let filename = common.extractFilename(responseData.filePath);
          if (responseData.dir) {
            that.data.filename.filename = filename;
            that.data.filename.extension = '';
          } else {
            let splitResult = common.splitFilenameExtension(filename);
            that.data.filename.filename = splitResult[0];
            that.data.filename.extension = splitResult[1];
          }
   
          let imageUrls = [];
          for (let imageCode of responseData.imageCodes) {
            imageUrls.push(IMAGE_URL + imageCode);
          }
          that.data.image.imageUrls = imageUrls;
          that.data.image.dimension = responseData.dimension;
          that.data.isDir = responseData.dir;
          that.data.hasNext = responseData.hasNext;
          that.queryParam.sessionId = responseData.sessionId;

          that.data.tag.selectedTags = {};
          that.data.needSavaChange = true;
          that.data.filingTime = 0;
        })
      },
      /**
       * 整理，调用整理的接口
       * */
      filing() {
        let that = this;
        let name = this.data.filename.filename + (this.data.filename.extension ? this.data.filename.extension : '');
        let tagIds = [];
        for (let tagId in this.data.tag.selectedTags) {
          tagIds.push(tagId);
        }
        if (that.data.filingTime == 0) {
          this.filingParam.name = name;
          this.filingParam.tagId = tagIds;
          http.post('/one-piece', this.filingParam, data => {
            that.updateParam.onePieceId = data;
            that.data.needSavaChange = false;
            that.data.filingTime += 1;
          })
        } else {
          this.updateParam.name = name;
          this.updateParam.tagId = tagIds;
          http.put('/one-piece', this.updateParam, data => {
            that.data.needSavaChange = false;
            that.data.filingTime += 1;
          });
        }
        for (let i = 0; i < tagIds.length; i++) {
          let tagId = tagIds[i];
          store.recentUsingTagManager.addCount(tagId, this.data.tag.selectedTags[tagId]);
        }
      },
    },
    watch: {
      '$route' (to, from) {
        this.init();
      }
    },
    mounted() {
      this.init();
    },
    data() {
      return {
        queryParam: {
          dirPath: null,
          fileType: null,
          pageNum: null,
          pageSize: null,
          index: null,
          sessionId: null
        },
        filingParam: {
          filePath: null,
          name: null,
          tagId: null,
        },
        updateParam: {
          onePieceId: null,
          name: null,
          tagId: null,
        },
        data: {
          isDir: null,
          hasNext: null,
          image: {
            dimension: {
              width: 0,
              height: 0
            }, // 防止第一次渲染时数据未返回导致的报错
            current: 0,
            imageUrls: null
          },
          filename: {
            filename: null,
            extension: null
          },
          tag: {
            selectedTags: {},
          },
          needSavaChange: true,
          filingTime: 0,
        }
      }
    }
  }
</script>

<style>
</style>
